<?php
namespace Frieza\Modules\Ifttt\Actions;

use Doraemon\service\Ifttt as sIfttt;
use Encore\Admin\Actions\RowAction;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;

class Copy extends RowAction
{
    public $name = '复制';

    public function handle($id, Request $request)
    {
        //屏蔽复制任务
        return $this->response()->error('复制失败：请使用新的V3创建和编辑任务');
        $task_name = trim($request->post('task_name'));
        $res = sIfttt::taskCopy($id, $task_name);
        if ($res) {
            return $this->response()->success('复制成功')->refresh();
        }else{
            return $this->response()->error('复制失败,请稍后重试');
        }
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function form($request)
    {
        $default_name = $request->taskName . "复制";
        $this->text('task_name', '任务名称')->default($default_name)->rules('required', ['required' => '任务名称不能为空']);
    }
}